package com.tsugun.mediaplayer.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.tsugun.mediaplayer.R;

/**
 * 多状态布局(使用多状态布局可以有效减少加载界面时消耗的资源和时间，达到使用时再加载资源的效果)
 * Created by shize on 2017/5/28.
 */

public class MultiStatusLayout extends RelativeLayout {

    public static final int STATUS_CONTENT = 0x00;
    public static final int STATUS_EMPTY = 0x01;
    public static final int STATUS_NO_NET = 0x02;
    public static final int STATUS_LOADING = 0x03;
    public static final int STATUS_ERROR = 0x04;

    private static final int NULL_RESOURCES_ID = -1;

    private View mContentView;
    private View mEmptyView;
    private View mNoNetworkView;
    private View mLoadingView;
    private View mErrorView;
    private int mContentViewId;
    private int mEmptyViewId;
    private int mNoNetworkViewId;
    private int mLoadingViewId;
    private int mErrorViewId;
    private int mViewStatus;

    private LayoutInflater mInflater;
    private final ViewGroup.LayoutParams mLayoutParams = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    public MultiStatusLayout(Context context) {
        this(context, null);
    }

    public MultiStatusLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiStatusLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs == null) {
            return;
        }
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiStatusLayout,
                defStyleAttr, 0);
        mContentViewId = a.getResourceId(R.styleable.MultiStatusLayout_contentLayout,
                NULL_RESOURCES_ID);
        mEmptyViewId = a.getResourceId(R.styleable.MultiStatusLayout_emptyLayout,
                R.layout.layout_multi_def_empty);
        mNoNetworkViewId = a.getResourceId(R.styleable.MultiStatusLayout_noNetworkLayout,
                R.layout.layout_multi_def_no_network);
        mLoadingViewId = a.getResourceId(R.styleable.MultiStatusLayout_loadingLayout,
                R.layout.layout_multi_def_loading);
        mErrorViewId = a.getResourceId(R.styleable.MultiStatusLayout_errorLayout,
                R.layout.layout_multi_def_error);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mInflater = LayoutInflater.from(getContext());
        showContentView();
    }

    /**
     * 获取控件状态
     *
     * @return status
     */
    public int getViewStatus() {
        return mViewStatus;
    }

    /**
     * 显示内容
     */
    public void showContentView() {
        showStatusView(STATUS_CONTENT, mContentView, mContentViewId);
    }

    /**
     * 显示数据为空提示
     */
    public void showEmptyView() {
        showStatusView(STATUS_EMPTY, mEmptyView, mEmptyViewId);
    }

    /**
     * 显示无网络
     */
    public void showNoNetworkView() {
        showStatusView(STATUS_NO_NET, mNoNetworkView, mNoNetworkViewId);
    }

    /**
     * 显示加载中
     */
    public void showLoadingView() {
        showStatusView(STATUS_LOADING, mLoadingView, mLoadingViewId);
    }

    /**
     * 显示错误提示
     */
    public void showErrorView() {
        showStatusView(STATUS_ERROR, mErrorView, mErrorViewId);
    }

    /**
     * 显示状态方法
     */
    private void showStatusView(int status, View v, int id) {
        resetView();
        mViewStatus = status;
        if (v == null && id != NULL_RESOURCES_ID) {
            v = mInflater.inflate(id, null);
            addView(v, 0, mLayoutParams);
        }
        showViewByStatus(mViewStatus);
    }

    /**
     * 清空多余view
     */
    private void resetView() {
        mContentView = null;
        mEmptyView = null;
        mErrorView = null;
        mLoadingView = null;
        mNoNetworkView = null;
        removeAllViews();
    }

    /**
     * 根据状态显示view
     */
    private void showViewByStatus(int status) {
        if (mContentView != null) {
            mContentView.setVisibility(status != STATUS_CONTENT ? View.GONE : View.VISIBLE);
        }
        if (mEmptyView != null) {
            mEmptyView.setVisibility(status != STATUS_EMPTY ? View.GONE : View.VISIBLE);
        }
        if (mNoNetworkView != null) {
            mNoNetworkView.setVisibility(status != STATUS_NO_NET ? View.GONE : View.VISIBLE);
        }
        if (mLoadingView != null) {
            mLoadingView.setVisibility(status != STATUS_LOADING ? View.GONE : View.VISIBLE);
        }
        if (mErrorView != null) {
            mErrorView.setVisibility(status != STATUS_ERROR ? View.GONE : View.VISIBLE);
        }
    }
}
